//
//  BDFeedbackViewController.m
//  BadBoy
//
//  Created by wen on 2016/12/26.
//  Copyright © 2016年 niusaibing. All rights reserved.
//

#import "BDFeedbackViewController.h"
#import <sys/utsname.h>
#import "BDTextView.h"

#define TITLE_LARGE_COUNT 50    //反馈内容字数限制

@interface BDFeedbackViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) BDTextView *textView;
@property (nonatomic, strong) UITextField *textfield;
@property (nonatomic, strong) UIButton *sendButton;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UILabel *numLabel;        //输入字数
@property (nonatomic, assign) int titleCount;          //内容剩余字数
@end

@implementation BDFeedbackViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = ASLocalizedString(@"意见反馈");
    self.view.backgroundColor = kBDColorGray;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.modalPresentationCapturesStatusBarAppearance = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    [self addContentView];
}

-(void)addContentView{
    _whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 150 + 80 + 44 + 20)];
    _whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_whiteView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, kBDScreenWidth- 20, 20)];
    label.textColor = KBDNameColor;
    label.font = [UIFont systemFontOfSize:17];
    [_whiteView addSubview:label];
    //手机型号
    //系统型号
    NSString *phoneVersion = [[UIDevice currentDevice] systemVersion];
    label.text = [NSString stringWithFormat:ASLocalizedString(@"设备：%@  iOS %@"),[self iphoneType],phoneVersion];
    //获取当前用户使用的版本号
    NSString *version= [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    UILabel *versionLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(label.frame)+5, kBDScreenWidth-20, 20)];
    versionLabel.textColor = KBDNameColor;
    versionLabel.font = [UIFont systemFontOfSize:17];
    versionLabel.text = [NSString stringWithFormat:ASLocalizedString(@"客户端版本：%@"),version];
    [_whiteView addSubview:versionLabel];
    _textView = [[BDTextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(versionLabel.frame)+5, kBDScreenWidth-20, 150)];
    _textView.placeholder = ASLocalizedString(@"在此输入反馈内容");
    _textView.delegate = self;
    _textView.layer.borderWidth = 1;
    [_textView setPlaceHolderFont:[UIFont systemFontOfSize:15]];
    _textView.layer.borderColor = kBDColorGray.CGColor;
    [_whiteView addSubview:_textView];
    _textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_textView.frame)+20, kBDScreenWidth- 20, kBDButtonHeight)];
    _textfield.placeholder = ASLocalizedString(@"请留下邮箱或电话，方便我们与您联系");
    _textfield.backgroundColor = [UIColor whiteColor];
    _textfield.layer.borderWidth = 1;
    _textfield.delegate = self;
    _textfield.layer.borderColor = kBDColorGray.CGColor;
    [_whiteView addSubview:_textfield];
    //显示还能输入多少字
    _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(kBDScreenWidth - 20 - 10 - 150, 150 - 20 - 10, 150, 20)];
    _numLabel.font = [UIFont systemFontOfSize:12];
    _numLabel.textColor = kBDTimeColor;
    _numLabel.textAlignment = NSTextAlignmentRight;
       [_numLabel setText:[NSString stringWithFormat:ASLocalizedString(@"您还可以输入%d个字"),(int)(TITLE_LARGE_COUNT-_textView.text.length)]];
    [_textView addSubview:_numLabel];
    
    _sendButton = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_whiteView.frame)+20, kBDScreenWidth - 40, kBDButtonHeight)];
    _sendButton.backgroundColor = KBDColorBlue;
    _sendButton.layer.cornerRadius = 5;
    [_sendButton setTitle:ASLocalizedString(@"提交")forState:UIControlStateNormal];
    [_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sendButton addTarget:self action:@selector(sendButtonPress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_sendButton];
}
-(void)sendButtonPress{
    // BDLog(ASLocalizedString(@"提交"));
    if (_titleCount<0) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:ASLocalizedString(@"反馈内容不能超过50个字，请把多余的字删除")message:nil delegate:nil cancelButtonTitle:ASLocalizedString(@"知道了")otherButtonTitles:nil];
        [alerView show];
    } else {
    }
}
#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    // BDLog(@"text %@",text);
    //当点击键盘上的完成按钮
    if ([text isEqualToString:@"\n"]) {
        [_textView resignFirstResponder];
        return NO;
    }
    //点击删除键时 range.length为1 点击其他键时 range.length为0
    if (range.length == 1) {
        return YES;
    }
    //限定textView的输入字数
    NSMutableString *editTitleViewText = [_textView.text mutableCopy];
    [editTitleViewText replaceCharactersInRange:range withString:text];
    return [editTitleViewText length] < TITLE_LARGE_COUNT+9;
}
- (void)textViewDidChange:(UITextView *)textView
{
    //计算出title的高度
    int titleHeight=0;
    if (isIos7System) {
        CGFloat fixedWidth = textView.frame.size.width;
        CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        titleHeight = newSize.height;
    } else {
        titleHeight = textView.contentSize.height;
    }
    //设置_numLabel里面显示的内容
    _titleCount = (int)(TITLE_LARGE_COUNT-_textView.text.length);
    [_numLabel setText:[NSString stringWithFormat:ASLocalizedString(@"您还可以输入%d个字"),_titleCount]];
}

#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [_textfield resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSString *)iphoneType{
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    
    if ([platform isEqualToString:@"iPod1,1"])  return @"iPod Touch 1G";
    
    if ([platform isEqualToString:@"iPod2,1"])  return @"iPod Touch 2G";
    
    if ([platform isEqualToString:@"iPod3,1"])  return @"iPod Touch 3G";
    
    if ([platform isEqualToString:@"iPod4,1"])  return @"iPod Touch 4G";
    
    if ([platform isEqualToString:@"iPod5,1"])  return @"iPod Touch 5G";
    
    if ([platform isEqualToString:@"iPad1,1"])  return @"iPad 1G";
    
    if ([platform isEqualToString:@"iPad2,1"])  return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,2"])  return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,3"])  return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,4"])  return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,5"])  return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad2,6"])  return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad2,7"])  return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad3,1"])  return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,2"])  return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,3"])  return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,4"])  return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad3,5"])  return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad3,6"])  return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad4,1"])  return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,2"])  return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,3"])  return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,4"])  return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"iPad4,5"])  return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"iPad4,6"])  return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"i386"])      return @"iPhone Simulator";
    
    if ([platform isEqualToString:@"x86_64"])    return @"iPhone Simulator";
    
    return platform;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
